﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Model;
using BLL;
using Utility;

public partial class VehicleUI_Fuel_CardFuelAMV : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["Type"] == null)
            {
                if (Session["FilterFrameNO"] != null)
                {
                    txt_FrameNO.Text = Session["FilterFrameNO"].ToString();
                }
            }
            else if (Request.QueryString["Type"].ToString() == "View")
            {
                LoadService();
                txt_VehicleID.Enabled = false;
                txt_ChargeTime.Enabled = false;
                txt_FrameNO.Enabled = false;
                txt_ChargeAmount.Enabled = false;
                txt_Balance.Enabled = false;


                btn_Sure.Visible = false;
            }
            else if (Request.QueryString["Type"].ToString() == "Edit")
            {
                LoadService();
            }
        }
    }


    protected void LoadService()
    {
        int _id = Convert.ToInt32(Request.QueryString["ID"]);
        CardFuel cs = new CardFuelBLL().GetModel(_id);
        if (cs != null)
        {
            txt_VehicleID.Text = cs.VehicleID;
            txt_FrameNO.Text = cs.FrameNO;
            txt_ChargeTime.Text = cs.ChargeTime.ToString();
            txt_ChargeAmount.Text = cs.ChargeAmount.ToString();
            txt_Balance.Text = cs.Balance.ToString();
        }
    }

    protected void btn_Sure_Click(object sender, EventArgs e)
    {
        string _frameNO = txt_FrameNO.Text.Trim();
        bool reExist = new VehicleBLL().Exists(_frameNO);
        if (!reExist)
        {
            UtilityService.Alert(this.Page, "不存在该车辆信息,请重新录入车架号!");
            txt_FrameNO.Focus();
            return;
        }

        if (!ValidateService.IsNumber(txt_ChargeAmount.Text.Trim()))
        {
            UtilityService.Alert(this.Page, "金额格式错误!");
            txt_ChargeAmount.Focus();
            return;
        }
        if (!ValidateService.IsNumber(txt_Balance.Text.Trim()))
        {
            UtilityService.Alert(this.Page, "金额格式错误!");
            txt_Balance.Focus();
            return;
        }



        if (Request.QueryString["Type"] == null)
        {
            CardFuel cs = new CardFuel();

            cs.FrameNO = txt_FrameNO.Text.Trim();
            cs.ChargeTime = DateTime.Parse(txt_ChargeTime.Text.Trim());
            cs.ChargeAmount = decimal.Parse(txt_ChargeAmount.Text.Trim());
            cs.Balance = decimal.Parse(txt_Balance.Text.Trim());
            cs.OrganID = (int)Session["OrganID"];
            cs.InputBy = Session["UserID"].ToString();

            int re = new CardFuelBLL().Add(cs);

            if (re > 0)
            {
                string msg = "";
                UtilityService.AlertAndRedirect(this.Page, "添加成功! " + msg, "CardFuelMgr.aspx");
            }
            else
            {

                UtilityService.Alert(this.Page, "添加失败! ");
            }
        }
        else if (Request.QueryString["Type"] == "Edit")
        {
            int _id = Convert.ToInt32(Request.QueryString["ID"]);
            CardFuel cs = new CardFuelBLL().GetModel(_id);
            cs.ID = _id;
            cs.FrameNO = txt_FrameNO.Text.Trim();
            cs.ChargeTime = DateTime.Parse(txt_ChargeTime.Text.Trim());
            cs.ChargeAmount = decimal.Parse(txt_ChargeAmount.Text.Trim());
            cs.Balance = decimal.Parse(txt_Balance.Text.Trim());

            bool re = new CardFuelBLL().Update(cs);

            if (re)
            {
                string msg = "";
                UtilityService.AlertAndRedirect(this.Page, "修改成功! " + msg, "CardFuelMgr.aspx");
            }
            else
            {
                UtilityService.Alert(this.Page, "修改失败! ");
            }
        }
    }

    protected void btn_Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("CardFuelMgr.aspx");
    }
}
